<?php
namespace Common\Util;

class LanzSMSDirectSendSMS {
	
	public $returnString = '';
	
	public $acitiveid = '';
	
	private $account = 'meididi';
	
	private $userId = '864892';
	
	private $password = '3700673220FB89CBF0C610E5624663A8ED523411';
	
	private $url = "/LANZGateway/DirectSendSMSs.asp";
	
	public function send($phone, $content) {
		$content = mb_convert_encoding('【美滴滴】'.$content, 'gbk', 'utf-8');//短信平台支持gb2312
		
		$params = array(
			'SMSType' => 1,
			'UserID' => $this->userId,
			'Account' => $this->account,
			'Password' => $this->password,
			'Phones' => $phone,
			'Content' => $content,
		);
		return $this->httpPost($this->url, http_build_query($params), '', 1);
	}
	
	public function httpPost($sURL, $aPostVars, $sessid, $nMaxReturn) {
		$srv_ip = '219.136.252.188'; // 你的目标服务地址或频道.
		$srv_port = 80;
		$url = $sURL; // 接收你post的URL具体地址
		$fp = '';
		$resp_str = '';
		$errno = 0;
		$errstr = '';
		$timeout = 300;
		$post_str = $aPostVars; // 要提交的内容.
		
		$fp = fsockopen ( $srv_ip, $srv_port, $errno, $errstr, $timeout );
		if (! $fp) {
			return false;
		}
	
		$content_length = strlen ( $post_str );
		$post_header = "POST $url HTTP/1.1\r\n";
		$post_header .= "Content-Type:application/x-www-form-urlencoded\r\n";
		$post_header .= "User-Agent: MSIE\r\n";
		$post_header .= "Host: " . $srv_ip . "\r\n";
		$post_header .= "Cookie: " . $sessid . "\r\n";
		$post_header .= "Content-Length: " . $content_length . "\r\n";
		$post_header .= "Connection: close\r\n\r\n";
		$post_header .= $post_str . "\r\n\r\n";
	
		fwrite ( $fp, $post_header );
	
		$inheader = 1;
		while ( ! feof ( $fp ) ) {
			$resp_str .= fgets ( $fp, 4096 ); // 返回值放入$resp_str
			if ($inheader && ($resp_str == "\n" || $resp_str == "\r\n")) {
				$inheader = 0;
			}
		}
	
		fclose ( $fp );
		if ($nMaxReturn == 0) {
			if (substr ( $resp_str, strpos ( $resp_str, "<ErrorNum>" ) + 10, strpos ( $resp_str, "</ErrorNum>" ) - strpos ( $resp_str, "<ErrorNum>" ) - 10 ) == 0) {
				$acitiveid = substr ( $resp_str, strpos ( $resp_str, "<ActiveID>" ) + 10, strpos ( $resp_str, "</ActiveID>" ) - strpos ( $resp_str, "<ActiveID>" ) - 10 );
				return false;
			}
			return true;
		} else {
			if (substr ( $resp_str, strpos ( $resp_str, "<ErrorNum>" ) + 10, strpos ( $resp_str, "</ErrorNum>" ) - strpos ( $resp_str, "<ErrorNum>" ) - 10 ) == 0) {
				return true;
			} else {
				$returnString = substr ( $resp_str, strpos ( $resp_str, "<ErrorNum>" ) + 10, strpos ( $resp_str, "</ErrorNum>" ) - strpos ( $resp_str, "<ErrorNum>" ) - 10 );
				return false;
			}
		}
	}
}